package lysjmaster.base.auth.controller;

import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;
import lysjmaster.base.auth.domain.Permission;
import lysjmaster.base.auth.service.PermissionService;
import lysjmaster.base.auth.vo.PermissionVO;
import lysjmaster.base.common.annotation.TokenInfo;
import lysjmaster.base.common.cache.RedisKeyEnum;
import lysjmaster.base.common.cache.RedisUtil;
import lysjmaster.base.common.spring.base.BaseController;
import lysjmaster.base.common.spring.base.BaseService;
import lysjmaster.base.common.spring.jpa.JpaQuery;
import lysjmaster.base.common.spring.pagination.PageRequestDTO;
import lysjmaster.base.common.web.ParamUtil;
import lysjmaster.base.common.web.Resp;
import lysjmaster.base.common.web.RespUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author Created by zk on 2019-12-05 3:40
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/auth/permission")
public class PermissionController extends BaseController<Permission, PageRequestDTO> {
    @Resource
    private PermissionService permissionService;
    @Resource
    private RedisUtil redisUtil;


    @Override
    public BaseService<Permission> getService() {
        return permissionService;
    }

    @GetMapping("/findMenuList")
    public Resp findMenuList(@TokenInfo String userId) {
        Optional<List<PermissionVO>> userPermissionsOp = redisUtil.get(RedisKeyEnum.USER_PERMISSION, userId);
        List<PermissionVO> userPermissions = userPermissionsOp.orElse(new ArrayList<>());
        if (ParamUtil.isBlank(userPermissions)) {
            userPermissions = permissionService.findMenuList(userId);
            if (!ParamUtil.isBlank(userPermissions)) {
                redisUtil.set(RedisKeyEnum.USER_PERMISSION, userId, userPermissions, 5, TimeUnit.MINUTES);
            }
        }
        return RespUtil.success(userPermissions);
    }

    @GetMapping("/find_all_list")
    public Resp findAllList() {
        List<Permission> permissions = permissionService.findAllList();
//        Specification specification = new Specification() {
//            @Override
//            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                List<Predicate> predicates = new LinkedList<>();
//                predicates.add(criteriaBuilder.like(root.get("name"),"%菜单%"));
//                predicates.add(criteriaBuilder.gt(root.get("level"),1));
//                final Predicate and = criteriaBuilder.or(ArrayUtil.toArray(predicates, Predicate.class));
//                predicates = new LinkedList<>();
//                predicates.add(criteriaBuilder.equal(root.get("delFlag"),1));
//                predicates.add(criteriaBuilder.like(root.get("id"),"%0%"));
//                Predicate and1 = criteriaBuilder.and(ArrayUtil.toArray(predicates, Predicate.class));
//
//                return criteriaBuilder.and(and,and1);
//            }
//        };
        JpaQuery.entity(Permission.class)
        return RespUtil.success(permissions);
    }

    @Override
    public Resp update(Permission entity) {
        permissionService.updatePermission(entity);
        return RespUtil.success("修改成功");
    }

    @Override
    public Resp delete(String[] ids) {
        permissionService.deletePermission(ids);
        return RespUtil.success("删除成功");
    }
}
